package com.iflytek.uoamp.agent.monitor.mcagent;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import com.iflytek.uoamp.agent.common.constants.CommonConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import com.iflytek.uoamp.agent.common.util.ReadFile;
import com.iflytek.uoamp.agent.monitor.lib.statics.Statics;
import com.iflytek.uoamp.agent.monitor.utils.WinIPFinder;
/**
 * 
 * @ClassName: MCAgentConfig
 * @Description: monitor模块配置信息
 * @author qinanyuan
 * @date 2017年3月23日 下午3:20:29
 */
public class MCAgentConfig {
	private static MCAgentConfig instance = new MCAgentConfig();
	private String monitoredPointIp;
	private String monitoredHostName;
	private String configIp;
	private Integer port;
	private String mainVersion = "3";
	private String subVersion;
	private String sendList;
	private int not_active_time;
	private int find_interval;
	private boolean isLinux;
	private String upload_url;
	// 机房ID
	private String engineRoomId;
	// 机房名称
	private String engineRoomName;
	// MAC地址
	private String monitoredPointMAC;
	// 自动探测本地ip开关
	private boolean autoDetectLocalIp = true;
	
	private MCAgentConfig() {

	}

	@SuppressWarnings("unused")
	private static List<Integer> caseString2FailedJobFrequencyList(String strs) {
		String[] list = strs.split(",");
		List<Integer> frequencyList = new ArrayList<Integer>();
		Set<String> set = new TreeSet<String>();
		for (String str : list) {
			set.add(str);
		}

		Object[] setArray = set.toArray();
		for (Object obj : setArray) {
			frequencyList.add(Integer.valueOf((String) obj));
		}

		Collections.sort(frequencyList, Collections.reverseOrder());
		return frequencyList;
	}



	public static MCAgentConfig getInstance() {
		return instance;
	}

	public int getFind_interval() {
		return find_interval;
	}

	public void setFind_interval(int find_interval) {
		this.find_interval = find_interval;
	}

	public int getNot_active_time() {
		return not_active_time;
	}

	public void setNot_active_time(int not_active_time) {
		this.not_active_time = not_active_time;
	}

	public String getVersion(){
		return (mainVersion+"."+subVersion).trim();
	}

	public void setVersion(String version){
		int index = version.indexOf('.');
		if(index!=-1){
			subVersion = version.substring(index+1).trim();
		}
	}

	public String getMainVersion() {
		return mainVersion.trim();
	}

	public void setMainVersion(String mainVersion) {
		this.mainVersion = mainVersion.trim();
	}

	public String getSubVersion() {
		return subVersion.trim().trim();
	}

	public void setSubVersion(String subVersion) {
		this.subVersion = subVersion.trim();
	}

	public String getSendList() {
		return sendList;
	}

	public void setSendList(String sendList) {
		this.sendList = sendList;
	}

	public String getMonitoredPointIp() {
		if (monitoredPointIp != null) {
			return monitoredPointIp.trim();
		} else {
			return null;
		}

	}

	public void setMonitoredPointIp(String monitored_point_ip) {
		this.monitoredPointIp = monitored_point_ip;
	}

	public String getConfigIp() {
		if (configIp == null) {
			return "null";
		}
		return configIp.trim();
	}

	public void setConfigIp(String configIp) {
		this.configIp = configIp;
	}

	public boolean isLinux() {
		return isLinux;
	}

	public void setLinux(boolean isLinux) {
		this.isLinux = isLinux;
	}

	/**
	 * init agent config
	 *
	 * @param path
	 * @throws IOException
	 */
	public void init(String path) throws IOException {
		// 加载默认的agent配置，端口，版本等；
		Properties prop = new Properties();
		BufferedReader in = null;
		
		// 加载自动发现的配置；
		try {
			Properties props = System.getProperties();
			String osName = props.getProperty("os.name");
			isLinux = !StringUtils.containsIgnoreCase(osName, "windows");
		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(), e);
		}
		
		// 拷贝本地硬件探测所需的动态连接库到指定位置
		if (isLinux) {
			String arch = System.getProperty("os.arch");
			Logger.getRootLogger().info("System arch is : " + arch);
			boolean system_64bit = StringUtils.containsIgnoreCase(arch, "64");
			String fileName = system_64bit ? CommonConstants.LIBSIGAR_LINUX_64BIT : CommonConstants.LIBSIGAR_LINUX_32BIT;
			String destPath = CommonConstants.LIBSIGAR_LINUX_SRCPATH + fileName;
			
			// 赋予执行权限
			try {
				String cmd = "chmod 700 -R " + destPath;
				Runtime.getRuntime().exec(cmd);
			} catch (IOException e) {
				Logger.getRootLogger().error("chmod 700 -R to " + fileName + " is error!");
			}
		}
		
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF-8"));
			prop.load(in);
			monitoredPointIp = prop.getProperty("monitored_point_ip", "");
			if ("".equals(monitoredPointIp.trim())) {
				monitoredPointIp = null;
			}
			configIp = monitoredPointIp;
			// 单位是分钟
			not_active_time = Integer.parseInt(prop.getProperty("not_active_time", "60"));
			find_interval = Integer.parseInt(prop.getProperty("find_interval", "10"));
			port = Integer.parseInt(prop.getProperty("port", "1235"));
			Statics.setInterval(Integer.parseInt(prop.getProperty("static_interval_time", "60")));
			subVersion = ReadFile.readAll(CommonConstants.VERSION_PATH, "utf-8");
			sendList = prop.getProperty("sendList");
			upload_url = prop.getProperty("upload_url");
			engineRoomId = prop.getProperty("engine_room_id");
			engineRoomName = prop.getProperty("engine_room_name", "");
			monitoredPointMAC = WinIPFinder.getMAC();
			monitoredHostName = WinIPFinder.getHostName();
			autoDetectLocalIp = Boolean.valueOf(prop.getProperty("autoDetectLocalIp", "true"));
		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(), e);
		} finally {
			if (in != null) {
				in.close();
			}
			prop.clear();
			if (StringUtils.isBlank(monitoredPointMAC)) {
				monitoredPointMAC = "";
			}
		}

		
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public String getUpload_url() {
		return upload_url;
	}

	public void setUpload_url(String upload_url) {
		this.upload_url = upload_url;
	}

	public String getEngineRoomId() {
		return engineRoomId;
	}

	public void setEngineRoomId(String engineRoomId) {
		this.engineRoomId = engineRoomId;
	}

	public String getEngineRoomName() {
		return engineRoomName;
	}

	public void setEngineRoomName(String engineRoomName) {
		this.engineRoomName = engineRoomName;
	}

	public String getMonitoredPointMAC() {
		return monitoredPointMAC;
	}

	public void setMonitoredPointMAC(String monitoredPointMAC) {
		this.monitoredPointMAC = monitoredPointMAC;
	}

	public String getMonitoredHostName() {
		return monitoredHostName;
	}

	public void setMonitoredHostName(String monitoredHostName) {
		this.monitoredHostName = monitoredHostName;
	}

	public boolean isAutoDetectLocalIp() {
		return autoDetectLocalIp;
	}

	public void setAutoDetectLocalIp(boolean autoDetectLocalIp) {
		this.autoDetectLocalIp = autoDetectLocalIp;
	}
	
	
}
